<template>
  <d2-container>
    <div class="row-ac pb-2">
      <div><el-button type="warning" @click="addDiamond" size="small" icon="el-icon-plus">添加价格</el-button></div>
    </div>
    <div class="mt-2" style="width: 50%;">
      <el-table :data="priceList" border stripe>
        <el-table-column align="center" label="序号" type="index" width="45" />
        <el-table-column align="center" label="钻石价格" prop="latest_price" width="160" />
        <el-table-column align="center" label="添加时间" prop="created_time" width="160" />
        <el-table-column align="center" label="操作" width="230">
          <template slot-scope="scope">
            <el-button size="mini" @click="handleEdit(scope.row.id)">修改</el-button>
            <el-button size="mini" type="danger" @click="openDelete(scope.row.id)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
    </div>
    <div class="flex-1 ml-2 mt-2">
      <el-pagination @size-change="handleSizeChange" @current-change="handleCurrentChange"
        :current-page.sync="queryInfo.page" layout="total, prev, pager, next" :total="priceTotal">
      </el-pagination>
    </div>
    <el-dialog :visible.sync="dialogVisible" width="30%" title="添加分类">
      <el-form :model="formData" ref="form" label-width="80px">
        <el-form-item label="钻石价格" prop="latest_price">
          <div style="width: 50%;">
            <el-input v-model="formData.latest_price"></el-input>
          </div>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="submitForm">确 定</el-button>
      </div>
    </el-dialog>
  </d2-container>
</template>

<script>
  import {
    marketpriceDeleteUrl,
    marketpriceUpdateUrl,
    marketpriceCreateUrl,
    marketpriceDetailUrl,
    marketpriceListUrl,
  } from '@/utils/takemoney'
  export default {
    data() {
      return {
        dialogVisible: false,
        addStatus: true,
        queryInfo: {
          page: 1,
          page_size: 10
        },
        priceList: [],
        priceTotal: 0,
        formData: {
          latest_price: ''
        },
        getDiamondId: ''
      }
    },
    created() {
      this.initData()
    },
    methods: {
      initData() {
        this.getMarketpriceList()
      },
      getMarketpriceList() {
        this.$get(marketpriceListUrl, this.queryInfo).then(res => {
          if (res.data.code == 200) {
            this.priceList = res.data.data.list
            this.priceTotal = res.data.data.total
          }
        })
      },
      // 分页页码显示条数
      handleSizeChange(newSize) {
        // 把每页显示多少数据重新赋值
        this.queryInfo.page_size = newSize
        this.getMarketpriceList()
      },
      // 翻页事件
      handleCurrentChange(newPage) {
        this.queryInfo.page = newPage
        this.getMarketpriceList()
      },
      addDiamond() {
        this.dialogVisible = true
        this.addStatus = true
      },
      submitForm() {
        if (this.addStatus) {
          this.$post(marketpriceCreateUrl, this.formData).then(res => {
            if (res.data.code == 200) {
              this.$message({
                type: 'success',
                message: '添加成功'
              });
              this.getMarketpriceList()
              this.dialogVisible = false
              this.formData = {}
            }
          })
        } else {
          this.$put(marketpriceUpdateUrl + this.getDiamondId + '/', this.formData).then(res => {
            if (res.data.code == 200) {
              this.$message({
                type: 'success',
                message: '修改成功'
              });
              this.getMarketpriceList()
              this.dialogVisible = false
              this.formData = {}
            }
          })
        }

      },
      handleEdit(diamondId) {
        this.dialogVisible = true
        this.addStatus = false
        this.$get(marketpriceDetailUrl + diamondId + '/').then(res => {
          if (res.data.code == 200) {
            this.getDiamondId = res.data.data.id
            this.formData = {
              latest_price: res.data.data.latest_price,
            }
          }
        })
      },
      openDelete(diamondId) {
        this.$confirm('此操作将永久删除, 是否继续?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          this.$del(marketpriceDeleteUrl + diamondId + '/').then(res => {
            if (res.data.code == 200) {
              this.$message({
                type: 'success',
                message: '删除成功!'
              });
              this.getMarketpriceList()
            }
          })
        }).catch(() => {
          this.$message({
            type: 'info',
            message: '已取消删除'
          });
        });
      }
    }
  }
</script>

<style>
</style>